package com.koron.tool.util;

import java.io.*;

/**
 * Created by Administrator on 2016/11/26.
 */
public class SerializeUtil {

    public static byte[] writeObject(Object obj)
    {
        try {
            if (null == obj) {
                return null;
            } else {
                ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
                ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
                objectOutputStream.writeObject(obj);
                return byteArrayOutputStream.toByteArray();


            }
        }catch (Throwable t){
            t.printStackTrace();
            throw new RuntimeException(t);
        }



    }


    public static Object readObject(byte[] data)
    {
        try {
            if (null == data) {
                return null;
            } else {
                ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(data);
                ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream);
                return objectInputStream.readObject();
            }
        }catch (Throwable t){
            t.printStackTrace();
            throw new RuntimeException(t);
        }
    }
}
